﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace GW2LastOnline.Controls
{
    class SearchBox : Panel
    {
        new public event EventHandler TextChanged;
        public event EventHandler ExpandClicked;

        private const int PADDING = 5;
        private TextBox textSearch;
        private ImageButton buttonExpand;

        private bool hintVisible;

        public SearchBox()
        {
            base.BorderStyle = BorderStyle.None;
            base.BackColor = Color.White;
            
            textSearch = new TextBox();
            textSearch.BackColor = Color.White;
            textSearch.BorderStyle = BorderStyle.None;
            textSearch.Location = new Point(PADDING, PADDING);

            textSearch.LostFocus += new EventHandler(textSearch_Leave);
            textSearch.TextChanged += new EventHandler(textSearch_TextChanged);

            buttonExpand = new ImageButton();
            buttonExpand.Size = Properties.Resources.buttonArrow.Size;
            buttonExpand.Image = Properties.Resources.buttonArrow;
            buttonExpand.ImageActive = Properties.Resources.buttonArrowHover;
            buttonExpand.ImagePressed = Properties.Resources.buttonArrow;
            buttonExpand.Cursor = System.Windows.Forms.Cursors.Hand;

            buttonExpand.Click += new EventHandler(buttonExpand_Click);

            ShowHint();

            this.Size = new Size(100, textSearch.Height);

            base.Controls.AddRange(new Control[]
            {
                textSearch,
                buttonExpand
            });
        }

        void buttonExpand_Click(object sender, EventArgs e)
        {
            if (ExpandClicked != null)
                ExpandClicked(this, e);
        }

        private void ShowHint()
        {
            if (hintVisible)
                return;

            hintVisible = true;

            textSearch.KeyDown += new KeyEventHandler(textSearch_KeyDown);
            textSearch.MouseDown += new MouseEventHandler(textSearch_MouseDown);

            textSearch.ForeColor = Color.LightGray;
            textSearch.Text = "Search...";
        }

        private void HideHint()
        {
            if (!hintVisible)
                return;

            textSearch.MouseDown -= textSearch_MouseDown;
            textSearch.KeyDown -= textSearch_KeyDown;

            textSearch.ForeColor = Color.Black;
            textSearch.Text = "";

            hintVisible = false;
        }

        new public string Text
        {
            get
            {
                if (hintVisible)
                    return "";
                return textSearch.Text;
            }
            set
            {
                if (value.Length > 0)
                {
                    HideHint();
                    textSearch.Text = value;
                }
                else
                    ShowHint();
            }
        }

        void textSearch_TextChanged(object sender, EventArgs e)
        {
            if (!hintVisible && TextChanged != null)
            {
                TextChanged(this, e);
            }
        }

        void textSearch_MouseDown(object sender, MouseEventArgs e)
        {
            HideHint();
        }

        void textSearch_KeyDown(object sender, KeyEventArgs e)
        {
            HideHint();
        }

        void textSearch_Leave(object sender, EventArgs e)
        {
            if (textSearch.TextLength == 0)
                ShowHint();
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (this.Visible)
            {
                try
                {
                    this.BeginInvoke(new MethodInvoker(delegate
                        {
                            if (hintVisible)
                                textSearch.Select(0, 0);
                            else
                                textSearch.Select(0, textSearch.TextLength);
                            textSearch.Focus();
                        }));
                }
                catch { }
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            textSearch.Size = new Size(this.Width - PADDING * 2 - buttonExpand.Width - PADDING, this.Height - PADDING * 2);
            buttonExpand.Location = new Point(this.Width - PADDING - buttonExpand.Width, this.Height / 2 - buttonExpand.Height / 2 + 1);
        }
    }
}
